package com.shen.search;

import java.io.File;
import java.io.IOException;
import java.util.Date;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;

/**
 * lucence3.6 HelloWorld
 * @author shen
 *
 */
public class HelloWorld {
	/**
	 * 创建索引
	 */
	public static void createIndexFile() {
		IndexWriter writer = null;
		try {
			// 创建系统文件,索引存放位置。
			Directory directory = new SimpleFSDirectory(new File("E://indexs"));
			// 需要的分词器
			Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);
			// 创建的是哪个版本的IndexWriterConfig
			IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_36,analyzer);
			
			writer = new IndexWriter(directory, conf);

			//造数据，把数据对象转成相应的Document，其中的属性转为Field。
			Document doc1 = new Document();
			Document doc2 = new Document();
			Document doc3 = new Document();
			Document doc4 = new Document();
			Document doc5 = new Document();
			Document doc6 = new Document();
			Document doc7 = new Document();
			Document doc8 = new Document();

			doc1.add(new Field("bookname", "钢铁是怎样炼成的", Store.YES,Index.ANALYZED));
			doc2.add(new Field("bookname", "英雄儿女", Store.YES, Index.ANALYZED));
			doc3.add(new Field("bookname", "格林童话", Store.YES, Index.ANALYZED));
			doc4.add(new Field("bookname", "钢和铁是两种金属", Store.YES,Index.ANALYZED));
			doc5.add(new Field("bookname", "钢和铁是两种金属", Store.YES,Index.ANALYZED));
			doc6.add(new Field("bookname", "白毛女", Store.YES, Index.ANALYZED));
			doc7.add(new Field("bookname", "钢的世界是钢", Store.YES, Index.ANALYZED));
			doc8.add(new Field("bookname", "钢铁战士", Store.YES, Index.ANALYZED));

			// 调用工具IndexWriter的addDocument(doc)，把Document添加到索引库中。
			writer.addDocument(doc1);
			writer.addDocument(doc2);
			writer.addDocument(doc3);
			writer.addDocument(doc4);
			writer.addDocument(doc5);
			writer.addDocument(doc6);
			writer.addDocument(doc7);
			writer.addDocument(doc8);
			
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if(null != writer){
				try {
					// 一定要关闭writer，这样索引才能写到磁盘上
					writer.close();
				} catch (CorruptIndexException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	/**
	 * 查询结果
	 * @throws IOException
	 */
	public static void searchIndexFile() throws IOException{
		//得到索引的目录
		Directory directory = new SimpleFSDirectory(new File("E://indexs"));
		IndexReader reader = IndexReader.open(directory);
		// 简单索引
		IndexSearcher searcher = new IndexSearcher(reader);
		Term t = new Term("bookname", "钢");
		//把要查询字符串转为Query对象,查询字符串也要先经过Analyzer（分词器）。要求搜索时使用的Analyzer要与建立索引时使用的Analzyer要一致，否则可能搜不出正确的结果。
		TermQuery quenry = new TermQuery(t);
		// 获得得分靠前的两个匹配记录
		ScoreDoc[] docs = searcher.search(quenry, 2).scoreDocs;
		for (int i = 0; i < docs.length; i++) {
			Date start = new Date();
			String bookname = searcher.doc(docs[i].doc).get("bookname");
			Date end = new Date();
			System.out.println(bookname + (end.getTime() - start.getTime())
					+ "ms");
		}
	}
	
	public static void main(String[] args) throws IOException{
		createIndexFile();
		searchIndexFile();
	}
}
